//
//  LHGuideViewController.m
//  Knight_Tour
//
//  Created by Bruce_Lin on 15/5/21.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHGuideViewController.h"
#import "LHGuideView.h"
#import "LHTabBarController.h"

@interface LHGuideViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain)LHGuideView *myScrollView;
@property (nonatomic, retain)LHGuideView *myScrollViewFont;
@property (nonatomic, retain)UIPageControl *page;
@property (nonatomic ,retain)NSTimer *myTimer;
@property (nonatomic ,assign)NSInteger moveCount;
@end

#define max 4

@implementation LHGuideViewController
- (void)dealloc
{
    [_myScrollView release];
    [_myScrollViewFont release];
    [_page release];
    [_myTimer release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _moveCount = 0;
    
    self.myScrollView = [[LHGuideView alloc]initWithFrame:self.view.bounds];
    [_myScrollView setContentSize:CGSizeMake(_myScrollView.frame.size.width * max, _myScrollView.frame.size.height)];
    [_myScrollView setContentOffset:CGPointMake(0, 0)];
    [_myScrollView setPagingEnabled:YES]; //开启翻页滚动
    _myScrollView.delegate = self;
    [self.view addSubview:_myScrollView];
    [_myScrollView release];
    
    self.myScrollViewFont = [[LHGuideView alloc]initWithFrame:self.view.bounds];
    [_myScrollViewFont setContentSize:CGSizeMake(_myScrollViewFont.frame.size.width * max, _myScrollViewFont.frame.size.height)];
    _myScrollViewFont.backgroundColor = [UIColor clearColor];
    [_myScrollViewFont setContentOffset:CGPointMake(0, 0)];
    [_myScrollViewFont setPagingEnabled:YES]; //开启翻页滚动
    _myScrollViewFont.delegate = self;
    [self.view addSubview:_myScrollViewFont];
    [_myScrollViewFont release];
    
    //创建页码控制器
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, self.view.frame.size.height - 30, 100, 30)];
    _page.numberOfPages = max;
    _page.currentPageIndicatorTintColor = [UIColor colorWithRed:0.466 green:1.000 blue:0.953 alpha:1.000];
    
    //添加响应事件
    [_page addTarget:self action:@selector(pageChange) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_page];
    [_page release];

    //创建背景图
    UIImageView *ImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _myScrollView.frame.size.width * max, _myScrollView.frame.size.height)];
    ImageView.image = [UIImage imageNamed:@"GuideBack"];
    [_myScrollView addSubview:ImageView];
    [ImageView release];
    
    //创建文字图
    for (NSInteger i = 0; i < 4; i ++) {
        
        NSString *name = [NSString stringWithFormat:@"%d0000",i + 1];
        UIImageView *img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:name]];
        [img1 setFrame:CGRectMake(self.view.frame.size.width / 2 - 220 / 2 + i * self.view.frame.size.width, self.view.frame.size.height / 2, 220, 40)];
        [self.myScrollViewFont addSubview:img1];
        [img1 release];
    }
    
    //创建按钮 点击进入主页
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(self.view.frame.size.width / 2 - 50 + self.view.frame.size.width * 3, self.view.frame.size.height - 100, 100, 50)];
    [btn setBackgroundImage:[UIImage imageNamed:@"007"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(startHome) forControlEvents:UIControlEventTouchUpInside];
    [self.myScrollViewFont addSubview:btn];

//    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
//
//    dispatch_async(globalQueue, ^{
//
//        //创建定时器 控制背景自动移动
//    });
    
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.005f target:self selector:@selector(movePic) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:_myTimer forMode:NSDefaultRunLoopMode];
    
    
}

- (void)movePic
{
    [_myScrollView setContentOffset:CGPointMake(_moveCount, 0)];
    
    if (_myScrollView.contentOffset.x == self.view.frame.size.width * 3) {
        
        [_myTimer invalidate];
    }
    _moveCount ++;
}

#pragma mark 页码控制器响应方法
- (void)pageChange
{
    [_myScrollViewFont setContentOffset:CGPointMake(_page.currentPage * 375, 0)];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _myScrollViewFont) {
        
        _page.currentPage = (_myScrollViewFont.contentOffset.x / _myScrollViewFont.frame.size.width);
    }
}


/**
 *  进入主界面
 */
- (void)startHome
{
    LHTabBarController *tbVC = [[LHTabBarController alloc]init];
    [self presentViewController:tbVC animated:YES completion:^{
        
    }];
    [tbVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
